package com.situ.blog.servlet;

import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.Type;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.service.IBlogService;
import com.situ.blog.service.ITypeService;
import com.situ.blog.service.impl.BlogServiceImpl;
import com.situ.blog.service.impl.TypeServiceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.MyBeanUtil;
import com.situ.blog.util.PageResult;
import com.situ.blog.util.Result;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private IBlogService blogService = new BlogServiceImpl();
    private ITypeService typeService = new TypeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("BlogServlet.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "toUpdate":
                toUpdate(req, resp);
                break;
            case "updateStatus":
                updateStatus(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case "exportExcel":
                exportExcel(req, resp);
                break;
            case "importExcel":
                importExcel(req, resp);
                break;

        }
    }

    private void importExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.importExcel");
        blogService.importExcel(req);
        JSONUtil.toJSON(resp, Result.ok("导出成功"));
    }

    private void exportExcel(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.exportExcel");
        blogService.exportExcel(resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.update");
        Blog blog = MyBeanUtil.copyToBean(req, Blog.class);
        blogService.update(blog);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.selectById");
        String id = req.getParameter("id");
        Blog blog = blogService.selectById(Integer.parseInt(id));
        List<Type> list = typeService.selectAll();
        Map<String, Object> map = new HashMap<>();
        map.put("blog", blog);
        map.put("list", list);
        JSONUtil.toJSON(resp, Result.ok(map));
    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        blogService.updateStatus(id, Integer.parseInt(status));
        JSONUtil.toJSON(resp, Result.ok("更新成功"));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.add");
        Blog blog = MyBeanUtil.copyToBean(req, Blog.class);
        blogService.add(blog);
        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    // /blog?method=selectByPage&page=1&limit=10&name=Java
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp)  {
        System.out.println("BlogServlet.selectByPage");
       /* String page = req.getParameter("page");
        Map<String, String[]> parameterMap = req.getParameterMap();
        Set<Map.Entry<String, String[]>> entries = parameterMap.entrySet();
        for (Map.Entry<String, String[]> entry : entries) {
            System.out.println(entry.getKey() + " : " + entry.getValue());
        }
        try {
            BeanUtils.populate(BlogQuery.class, req.getParameterMap());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }*/
        BlogQuery blogQuery = MyBeanUtil.copyToBean(req, BlogQuery.class);
        System.out.println(blogQuery);
        PageResult pageResult = blogService.selectByPage(blogQuery);

        JSONUtil.toJSON(resp, pageResult);
    }

    //http://localhost:8080/blog?method=toList
    //http://localhost:8080/page/blog/update
    //http://localhost:8080/page/blog/list
    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BlogServlet.toUpdate");
        req.getRequestDispatcher("/WEB-INF/blog_update.jsp").forward(req, resp);
    }

    private void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("BlogServlet.toList");
        req.getRequestDispatcher("/WEB-INF/blog_list.jsp").forward(req, resp);
    }
}
